397. 整数替换
https://leetcode-cn.com/problems/integer-replacement/
- 提交时间:2021-11-19 15:21:07
- 执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
- 内存消耗:1.9 MB, 在所有 Go 提交中击败了77.78%的用户
- 通过测试用例:47 / 47
func integerReplacement(n int) (ans int) {
for {
if n == 1 {
break
}
if n%2 == 0 {
n = n / 2
ans++
continue
}
if n%4 == 1 {
n = n - 1
ans++
continue
}
if n == 3 {
ans += 2
break
}
n = n + 1
ans++
}
return ans
}